<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-row style="text-align: right">
        <el-button type="primary" size="small" @click="showForm"
          >添加</el-button
        >
      </el-row>
      <el-table :data="list" stripe>
        <el-table-column type="index" width="50" label="序号"></el-table-column>
        <el-table-column label="资源名称" prop="title"></el-table-column>
        <el-table-column label="资源链接" prop="url"></el-table-column>
        <el-table-column label="获取码" prop="code"></el-table-column>
        <el-table-column label="所属分类" prop="categoryId">
          <template slot-scope="scope">
            {{categoryList.filter(item=>item.id == scope.row.categoryId)[0].title}}
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="审核状态" prop="status">
          <template slot-scope="scope">
            {{scope.row.status == 1 ? '通过':'未通过'}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="gmtCreate"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="info"
              @click="handleUpdate(scope.row.id)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      title="保存"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="handleClose"
    >
      <SaveFrom @done="done" :resourceId="resourceId" :close="dialogVisible"/>
    </el-dialog>
  </div>
</template>

<script>
import resourceApi from "@/api/resource";
import categoryApi from '@/api/category'
import SaveFrom from "./save";
export default {
  name: "resource",
  components: {
    SaveFrom,
  },
  data() {
    return {
      categoryList:[],
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
      },
      list: [],
      dialogVisible: false,
      resourceId: "",
    };
  },
  created() {
    this.getCategory()
  },
  methods: {
    // 获取分类
    async getCategory(){
      let res = await categoryApi.list({pageNum:1,pageSize:99});
      if(res.success){
        this.categoryList = res.data.records
        this.getList()
      }else{
        this.options = []
        return this.$message.error(res.msg)
      }
    },
    done() {
      this.dialogVisible = false;
      this.getList();
    },
    close() {
      this.dialogVisible = false;
    },
    async getList() {
      let res = await resourceApi.list(this.page);
      if (res.success) {
        this.list = res.data.records;
        this.page.total = res.data.total
      } else {
        return this.$message.error(res.msg);
      }
    },
    handleCurrentChange(newNum) {
      this.page.pageNum = newNum;
      this.getList();
    },
    handleSizeChange(newSize) {
      this.page.newSize = newSize;
      this.getList();
    },
    showForm() {
      this.resourceId = ""
      this.dialogVisible = true;
    },
    handleClose() {
      this.resourceId = "";
      this.dialogVisible = false;
    },
    handleUpdate(id) {
      this.resourceId = id
      this.dialogVisible = true;
    },
    async handleDel(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await resourceApi.deletes(id)
          if (res.success) {
            this.$message.success("删除成功");
            this.getList();
          } else {
            return this.$message.error(res.msg);
          }
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
</style>